//index.js
//引用接口
import http from '../../../class/Utils/Http.js'
import tips from '../../../class/Utils/Tips.js'

Page({
  data: {
    flag:0,
    terms:'',
    list:[],
    searchtag: 0,
    page:1,
    s:1
  },
  onLoad: function (get) {
    this.setData({
      terms: get.terms
    })
    this.getdata();
  },
  getdata:function(){
    var that = this
    that.setData({
      page:1,
    })
    //获取接口数据
    var data = {
      terms: that.data.terms,
    }
    http.reqload("/company/search", data, function (res) {
      console.log(res.list_products)

      //开始判断 有没有 http begin
      var newlistproducts = []
      for(var i = 0; i < res.list_products.length; i++){
        if (res.list_products[i].pic1 && !/http/.test(res.list_products[i].pic1)) {
          res.list_products[i].pic1 = "http://img.i.album.toocle.com/230-230/" + res.list_products[i].pic1
        }
        newlistproducts.push(res.list_products[i])
        
      }
    
      //开始判断 有没有 http end

      that.setData({
        list: newlistproducts,
        s: res.list_products.length
      })
    })
    wx.pageScrollTo({
      scrollTop: 0,
      duration:0
    })
  },
  //点击切换形式
  changearray:function(e){
    this.setData({
      flag: Math.abs(this.data.flag-1)
    })
  },
  //搜索框聚焦
  focusBlur:function(e){
    this.setData({
      searchtag: Math.abs(this.data.searchtag-1),
    })
  },
  //获取搜索框内容
  bindKeyInput: function (e) {
    this.setData({
      terms: e.detail.value
    })
  },
  //去首页
  gotohome:function(){
    wx.switchTab({
      url: '/pages/index/index'
    })
  },
  //去微商机
  gotoweishangji: function () {
    wx.switchTab({
      url: '/pages/weishangji/list/list'
    })
  },
  //去生意圈
  gotoshengyiquan: function () {
    wx.switchTab({
      url: '/pages/shengyiquan/list/list'
    })
  },
  //去会员中心
  gotomaincenter: function () {
    wx.switchTab({
      url: '/pages/main/menu/menu'
    })
  },
  onReachBottom: function () {
    var that = this
    var data = {
      terms: that.data.terms,
      page: ++that.data.page
    }
    console.log(data)
    var list = that.data.list
    http.reqload("/company/search", data, function (res) {
      //console.log(res);
      var newlist = res.list_products
      for (var i=0; i < newlist.length; i++){
         //console.log(newlist[i])
        if (newlist[i].pic1 && !/http/.test(newlist[i].pic1)) {
          newlist[i].pic1 = "http://img.i.album.toocle.com/230-230/" + newlist[i].pic1
        }
         list.push(newlist[i])
      }
      that.setData({
        list: list
      })
    })
  },
  
})
